﻿/************************************************************************

 This file is part of "10 Second Tim".

 "10 Second Tim" is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 "10 Second Tim" is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with "10 Second Tim".  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2013 Scott Bevin. all rights reserved

using System;
using LDE;
using LDE.Input;
using LudumDare27.Games;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LudumDare27.GameScreens
{
	internal class GameOverScreen : GameScreen
	{
		private SpriteFont font;
		private SpriteFont hintFont;
		private int score;
		private Texture2D logoTexture;

		private WhiteFadeScreen whiteFadeOut = null;
		private GameScreen gamescreenToTrigger = null;

		private Menu menu;

		private string[ ] hints =
        {
            "Don't forget to vote!",
            "Don't forget to vote!",
            "Don't forget to vote!",
			"Twitter: @gameXcore.",
			"Email me if you liked this: scott@gameXcore.co.uk.",
			"Chroma By Scott Bevin for LD27, www.gameXcore.co.uk",
            "Don't forget to vote!",
            "Don't forget to vote!",
        };

		private enum HintState
		{
			FadingIn,
			WaitingIn,
			FadingOut,
			WaitingOut,
		}

		private HintState hintState = HintState.FadingIn;
		private float hintTimer = 0.0f;
		private string hintStr = "";
		private float hintAlpha;

		private Random rnd = new Random( );

		private bool Won;

		public GameOverScreen( int score, bool won )
			: base( true )
		{
			this.Won = won;
			this.score = score;
		}

		public override void OnAdded( )
		{
			font = ContentManager.Load<SpriteFont>( "Fonts/DebugFont" );
			hintFont = ContentManager.Load<SpriteFont>( "Fonts/DebugFont" );
			logoTexture = ContentManager.Load<Texture2D>( "Img/GameLogo" );

			menu = new Menu( ContentManager.Load<SpriteFont>( "Fonts/DebugFont" ), ContentManager.Load<SpriteFont>( "Fonts/DebugFont" ), ContentManager );
			string text;

			if ( Won )
				text = String.Format( "You surived {0} reality jumps and made it back alive!", score );
			else
				text = String.Format( "Game Over! You surived {0} reality jumps!", score );

			menu.AddNonClickEntry( text, null );
			menu.AddNonClickEntry( "", null );
			menu.AddEntry( "Play Again", this.Rety );
			menu.AddEntry( "Quit To Main Menu", this.MainMenu );

#if !SILVERLIGHT
			menu.AddEntry( "Quit Game", this.QuitGame );
#endif

			hintStr = hints[ rnd.Next( hints.Length ) ];

			base.OnAdded( );
		}

		public override void Update( float dt )
		{
			menu.Update( dt, Player.Get.GetInput( this ), Game.GraphicsDevice );

			/*if (whiteFadeOut == null)
			{
				if (input.MouseButtonStates[(int)InputManager.MouseButtons.LeftButton].Released ||
					input.MouseButtonStates[(int)InputManager.MouseButtons.RightButton].Released)
				{
					whiteFadeOut = new WhiteFadeScreen();
					gamescreenToTrigger = new MainMenuScreen();

					GameScreenManager.AddGameScreen(whiteFadeOut, null, LudumDare26.GameScreenManager.AddGameScreenData.PositioningOptions.OnTop);
				}
			}*/

			if ( whiteFadeOut != null && whiteFadeOut.State == WhiteFadeScreen.States.FadedIn )
			{
				whiteFadeOut.FadeOut( );
				GameScreenManager.AddGameScreen( gamescreenToTrigger, whiteFadeOut, GameScreenManager.AddGameScreenData.PositioningOptions.BelowRef );
				this.KillScreen = true;
			}

			ProcessHint( dt );

			base.Update( dt );
		}

		private void ProcessHint( float dt )
		{
			if ( hintState == HintState.FadingIn )
			{
				hintTimer += dt;
				hintAlpha = hintTimer / 1.0f;

				if ( hintTimer >= 1.0f )
				{
					hintState = HintState.WaitingIn;
					hintTimer = 0.0f;
				}
			}
			else if ( hintState == HintState.WaitingIn )
			{
				hintTimer += dt;
				hintAlpha = 1.0f;

				if ( hintTimer >= 5.0f )
				{
					hintState = HintState.FadingOut;
					hintTimer = 0.0f;
				}
			}
			else if ( hintState == HintState.FadingOut )
			{
				hintTimer += dt;
				hintAlpha = 1.0f - hintTimer / 1.0f;

				if ( hintTimer >= 1.0f )
				{
					hintState = HintState.WaitingOut;
					hintTimer = 0.0f;
				}
			}
			else if ( hintState == HintState.WaitingOut )
			{
				hintTimer += dt;
				hintAlpha = 0.0f;

				if ( hintTimer >= 0.5f )
				{
					hintStr = hints[ rnd.Next( hints.Length ) ];
					hintState = HintState.FadingIn;
					hintTimer = 0.0f;
				}
			}
		}

		public override void Render( float dt, GraphicsDevice gd, SpriteBatch sprites )
		{
			Vector2 basePosition = new Vector2( ( (float)gd.PresentationParameters.BackBufferWidth ) * 0.5f,
				( (float)gd.PresentationParameters.BackBufferHeight ) * 0.25f );

			sprites.Begin( SpriteSortMode.Deferred, BlendState.NonPremultiplied );
			sprites.Draw( logoTexture, basePosition, null, Color.White, 0.0f,
				new Vector2( ( (float)logoTexture.Width ) * 0.5f, ( (float)logoTexture.Height ) * 0.5f ), Vector2.One, SpriteEffects.None, 0.0f );

			//string text = String.Format("Game Over! You got to level {0}!", score + 1);

			//sprites.DrawString(font, text, new Vector2(500.0f, 200.0f), Color.Black, 0.0f,
			//	font.MeasureString(text) * 0.5f, 1.0f, SpriteEffects.None, 0.0f);

			string text = hintStr;

			basePosition.Y = ( (float)gd.PresentationParameters.BackBufferHeight ) * 0.9f;
			sprites.DrawString( hintFont, text, basePosition, Color.Black * hintAlpha, 0.0f,
				hintFont.MeasureString( text ) * 0.5f, 1.0f, SpriteEffects.None, 0.0f );

			sprites.End( );

			menu.Render( gd, sprites );

			base.Render( dt, gd, sprites );
		}

		private void Rety( Menu.MenuEntry entry )
		{
			whiteFadeOut = new WhiteFadeScreen( );
			gamescreenToTrigger = new GameplayIntroScreen( false, 0 );

			GameScreenManager.AddGameScreen( whiteFadeOut, null, GameScreenManager.AddGameScreenData.PositioningOptions.OnTop );
		}

		private void MainMenu( Menu.MenuEntry entry )
		{
			whiteFadeOut = new WhiteFadeScreen( );
			gamescreenToTrigger = new MainMenuScreen( );

			GameScreenManager.AddGameScreen( whiteFadeOut, null, GameScreenManager.AddGameScreenData.PositioningOptions.OnTop );
		}

		private void QuitGame( Menu.MenuEntry entry )
		{
			this.Game.Exit( );
		}
	}
}